package com.jiutianniao.p2p.acctrans.core.facade.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.jiutianniao.p2p.acctrans.core.constants.AccountTransConst;
import com.jiutianniao.p2p.acctrans.core.dal.model.AccountDO;
import com.jiutianniao.p2p.acctrans.core.service.AccountFreezeLogService;
import com.jiutianniao.p2p.acctrans.core.service.AccountLogService;
import com.jiutianniao.p2p.acctrans.core.service.AccountService;
import com.jiutianniao.p2p.acctrans.core.util.ConverUtil;
import com.jiutianniao.p2p.base.web.page.PageVo;
import com.jiutianniao.p2p.trans.client.enums.AccountTypeEnum;
import com.jiutianniao.p2p.trans.client.facade.AccountQueryFacade;
import com.jiutianniao.p2p.trans.client.model.AccountFreezeLogQueryDto;
import com.jiutianniao.p2p.trans.client.model.AccountLogQueryDto;
import com.jiutianniao.p2p.trans.client.model.AccountLogVO;
import com.jiutianniao.p2p.trans.client.model.AccountVO;
import com.jiutianniao.p2p.trans.client.result.AccountQueryResult;

@WebService(endpointInterface = "com.jiutianniao.p2p.trans.client.facade.AccountQueryFacade", serviceName = "AccountQueryFacade")
@Service("accountQueryFacade")
public class AccountQueryFacadeImpl implements AccountQueryFacade {

	@Resource
	AccountService accountService;

	@Resource
	AccountLogService accountLogService;

	@Resource
	AccountFreezeLogService accountFreezeLogService;

	@Override
	public AccountQueryResult getAccountInfo(String accountNo) {
		AccountQueryResult result = new AccountQueryResult();
		AccountDO accountDO = accountService.getAccountDO(accountNo);
		if (accountDO == null) {
			result.setResultCode("account no exit");
			return result;
		}
		AccountVO accountVO = ConverUtil.converAccountDO2VO(accountDO);
		result.setAccountVO(accountVO);
		result.setSuccess(true);
		return result;
	}

	@Override
	public AccountQueryResult getAccountByUserId(Long userId) {
		String accountNo = AccountTransConst.yiqihao_account_prefix + "0"
				+ AccountTypeEnum.INVESTORS_ACCOUNT.getCode() + userId + "0"
				+ AccountTransConst.RMB_CURRENCY;
		return this.getAccountInfo(accountNo);
	}

	@Override
	public AccountQueryResult queryAccountLog(String accountNo) {
		AccountQueryResult result = new AccountQueryResult();
		List<AccountLogVO> logList = accountLogService.findLogList(accountNo);
		result.setAccountLogList(logList);
		result.setSuccess(true);
		return result;
	}

	@Override
	public PageVo queryLogPageByConditions(
			AccountLogQueryDto accountLogQueryDto) {

		PageVo page = accountLogService
				.queryLogPageByConditions(accountLogQueryDto);

		return page;

	}

	/*
	 * @Override public AccountQueryResult queryLogListByConditions(
	 * AccountLogQueryDto accountLogQueryDto) {
	 * 
	 * AccountQueryResult result = new AccountQueryResult(); List<AccountLogVO>
	 * logList = accountLogService
	 * .queryLogListByConditions(accountLogQueryDto);
	 * result.setAccountLogList(logList); result.setSuccess(true); return
	 * result; }
	 */

	public PageVo queryFreezeLogPageByConditions(
			AccountFreezeLogQueryDto accountFreezeLogQueryDto) {

		//AccountQueryResult result = new AccountQueryResult();
		PageVo page = accountFreezeLogService
				.queryFreezeLogPageByConditions(accountFreezeLogQueryDto);

		/*accountFreezeLogQueryDto.setTotalCount(page.getTotalCount());
		accountFreezeLogQueryDto.setLimit(page.getPageSize());
		accountFreezeLogQueryDto.setPgNumber(page.getPageNo());
		accountFreezeLogQueryDto
				.setAccountFreezeLogList((List<AccountFreezeLogVO>) page
						.getRows());

		result.setAccountFreezeLogQueryDto(accountFreezeLogQueryDto);
		result.setSuccess(true);*/
		return page;

	}

	@Override
	public AccountQueryResult batchQueryAccountInfo(List<String> accountNoList) {
		AccountQueryResult result = new AccountQueryResult();

		Map<String, AccountVO> accountVOMap = new HashMap<String, AccountVO>();
		if (CollectionUtils.isNotEmpty(accountNoList)) {
			List<AccountDO> accountDOList = accountService
					.queryByNoList(accountNoList);

			for (AccountDO accountDO : accountDOList) {
				AccountVO accountVO = ConverUtil.converAccountDO2VO(accountDO);
				accountVOMap.put(accountDO.getAccountNo(), accountVO);
			}
		}

		result.setSuccess(true);
		result.setAccountVOMap(accountVOMap);
		return result;
	}

}
